package com.project.seckill.config;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 * @date 2022-09-03 9:24
 */
@Configuration
public class RedisClusterPlusConfig {
    @Resource
    private RedisProperties redisProperties;

    /**
     * 连接池配置信息
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        RedisProperties.Pool jedisPool = redisProperties.getJedis().getPool();
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(jedisPool.getMaxIdle());
        poolConfig.setMinIdle(jedisPool.getMinIdle());
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        //其他属性可以自行添加
        return poolConfig;
    }

//    @Bean
//    public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
//        //设置redis服务器的host或者ip地址
//        standaloneConfiguration.setHostName("host");
//        standaloneConfiguration.setPort(1);
//
//        // 获得默认的连接池构造
//        // 这里需要注意的是，redisConnectionFactory对于Standalone模式的没有
//        // （RedisStandaloneConfiguration，JedisPoolConfig）的构造函数，对此
//        // 我们用JedisClientConfiguration接口的builder方法实例化一个构造器，还得类型转换
//        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcf = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
//        // 修改我们的连接池配置
//        jpcf.poolConfig(jedisPoolConfig);
//        // 通过构造器来构造jedis客户端配置
//        JedisClientConfiguration jedisClientConfiguration = jpcf.build();
//        return new JedisConnectionFactory(standaloneConfiguration, jedisClientConfiguration);
//    }
}
